using UnityEngine;

public class RCCWheelCollider : MonoBehaviour
{
	private RCCCarControllerV2 carController;

	private Rigidbody carRigid;

	private float startSlipValue = 0.25f;

	private RCCSkidmarks skidmarks;

	private int lastSkidmark = -1;

	private WheelCollider wheelCollider;

	private float wheelSlipAmountSideways;

	private float wheelSlipAmountForward;

	private WheelFrictionCurve forwardFrictionCurve;

	private WheelFrictionCurve sidewaysFrictionCurve;

	private void Start()
	{
		wheelCollider = GetComponent<WheelCollider>();
		carController = GetComponentInParent<RCCCarControllerV2>();
		carRigid = carController.GetComponent<Rigidbody>();
		if ((bool)UnityEngine.Object.FindObjectOfType(typeof(RCCSkidmarks)))
		{
			skidmarks = (UnityEngine.Object.FindObjectOfType(typeof(RCCSkidmarks)) as RCCSkidmarks);
		}
		else
		{
			UnityEngine.Debug.Log("No skidmarks object found. Skidmarks will not be drawn. Drag ''RCCSkidmarksManager'' from Prefabs folder, and drop on to your existing scene...");
		}
		forwardFrictionCurve = GetComponent<WheelCollider>().forwardFriction;
		sidewaysFrictionCurve = GetComponent<WheelCollider>().sidewaysFriction;
	}

	private void FixedUpdate()
	{
		if ((bool)skidmarks)
		{
			wheelCollider.GetGroundHit(out WheelHit hit);
			wheelSlipAmountSideways = Mathf.Abs(hit.sidewaysSlip);
			wheelSlipAmountForward = Mathf.Abs(hit.forwardSlip);
			if (wheelSlipAmountSideways > startSlipValue || wheelSlipAmountForward > 0.5f)
			{
				Vector3 pos = hit.point + 2f * carRigid.velocity * Time.deltaTime;
				if (carRigid.velocity.magnitude > 1f)
				{
					lastSkidmark = skidmarks.AddSkidMark(pos, hit.normal, wheelSlipAmountSideways / 2f + wheelSlipAmountForward / 2.5f, lastSkidmark);
				}
				else
				{
					lastSkidmark = -1;
				}
			}
			else
			{
				lastSkidmark = -1;
			}
		}
		wheelCollider.GetGroundHit(out WheelHit hit2);
		if (!wheelCollider.GetGroundHit(out hit2))
		{
			return;
		}
		if (carController._groundMaterial == RCCCarControllerV2.GroundMaterial.Asphalt && (bool)carController.asphaltPhysicsMaterial)
		{
			forwardFrictionCurve.stiffness = carController.asphaltPhysicsMaterial.staticFriction * 1.666f;
			sidewaysFrictionCurve.stiffness = carController.asphaltPhysicsMaterial.staticFriction * 1.666f;
		}
		else if (carController._groundMaterial == RCCCarControllerV2.GroundMaterial.Grass && (bool)carController.grassPhysicsMaterial)
		{
			forwardFrictionCurve.stiffness = carController.grassPhysicsMaterial.staticFriction * 1.666f;
			sidewaysFrictionCurve.stiffness = carController.grassPhysicsMaterial.staticFriction * 1.666f;
		}
		else if (carController._groundMaterial == RCCCarControllerV2.GroundMaterial.Sand && (bool)carController.sandPhysicsMaterial)
		{
			forwardFrictionCurve.stiffness = carController.sandPhysicsMaterial.staticFriction * 1.666f;
			sidewaysFrictionCurve.stiffness = carController.sandPhysicsMaterial.staticFriction * 1.666f;
		}
		else if ((bool)carController.asphaltPhysicsMaterial)
		{
			forwardFrictionCurve.stiffness = carController.asphaltPhysicsMaterial.staticFriction * 1.666f;
			sidewaysFrictionCurve.stiffness = carController.asphaltPhysicsMaterial.staticFriction * 1.666f;
		}
		else
		{
			forwardFrictionCurve.stiffness = 1f;
			sidewaysFrictionCurve.stiffness = 1f;
		}
		if (carController._wheelTypeChoise == RCCCarControllerV2.WheelType.DRIFT)
		{
			if (wheelCollider == carController.FrontLeftWheelCollider || wheelCollider == carController.FrontRightWheelCollider)
			{
				sidewaysFrictionCurve.asymptoteValue = 0.75f;
			}
			else
			{
				forwardFrictionCurve.asymptoteValue = 1f;
				sidewaysFrictionCurve.asymptoteValue = 0.65f;
			}
		}
		wheelCollider.forwardFriction = forwardFrictionCurve;
		wheelCollider.sidewaysFriction = sidewaysFrictionCurve;
	}
}
